package com.example.leetcode;

public class Leetcode21 {

     public static class ListNode {
         int val;
         ListNode next;
         ListNode() {}
         ListNode(int val) { this.val = val; }
         ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     }

    public static ListNode  mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode ret=new ListNode(0);
        ListNode next=ret;
        while (l1!=null &&  l2!=null){
            if(l1.val< l2.val){
                next.next=l1;
                l1=l1.next;
            }else {
                next.next=l2;
                l2=l2.next;
            }
            next=next.next;
        }
        if(l1!=null){
            next.next=l1;
        }
          if(l2!=null){
              next.next=l2;
        }
        return ret.next;
    }
    public static void main(String[] args) {

        ListNode head1=new ListNode(1);
        ListNode head2=new ListNode(2);
        head1.next=head2;

        ListNode head3=new ListNode(3);
        head2.next=head3;

        ListNode head11=new ListNode(1);
        ListNode head21=new ListNode(2);
        head11.next=head21;

        ListNode head31=new ListNode(3);
        head21.next=head31;
        ListNode listNode = mergeTwoLists(head1, head11);
        System.out.println(listNode.val);
    }
}
